#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :png.py
@说明    :处理图片的主要逻辑
@时间    :2021/02/03 16:01:49
@作者    :DanceDJ
@微信公众号    :给我一碗炒饭
@版本    :1.0
'''
import os, math,sys
from PIL import Image
import os
data=[{"name":"葡萄","file":"ad16ccdc-975e-4393-ae7b-8ac79c3795f2.png","path":"ad"},
{"name":"樱桃","file":"0cbb3dbb-2a85-42a5-be21-9839611e5af7.png","path":"0c"},
{"name":"橘子","file":"d0c676e4-0956-4a03-90af-fee028cfabe4.png","path":"d0"},
{"name":"柠檬","file":"74237057-2880-4e1f-8a78-6d8ef00a1f5f.png","path":"74"},
{"name":"猕猴桃","file":"132ded82-3e39-4e2e-bc34-fc934870f84c.png","path":"13"},
{"name":"西红柿","file":"03c33f55-5932-4ff7-896b-814ba3a8edb8.png","path":"03"},
{"name":"桃","file":"665a0ec9-6c43-4858-974c-025514f2a0e7.png","path":"66"},
{"name":"菠萝","file":"84bc9d40-83d0-480c-b46a-3ef59e603e14.png","path":"84"},
{"name":"椰子","file":"5fa0264d-acbf-4a7b-8923-c106ec3b9215.png","path":"5f"},
{"name":"半西瓜","file":"564ba620-6a55-4cbe-a5a6-6fa3edd80151.png","path":"56"},
{"name":"西瓜","file":"5035266c-8df3-4236-8d82-a375e97a0d9c.png","path":"50"}]
 
def app_path():
    """Returns the base application path."""
    if hasattr(sys, 'frozen'):
        # Handles PyInstaller
        return os.path.dirname(sys.executable)  #使用pyinstaller打包后的exe目录
    return os.path.dirname(__file__)                 #没打包前的py目录

tmd = app_path() # 这是解压路径
cwd = os.getcwd() # 这是程序的所在路径
out_path=tmd+"/output/"

def circle(p_path,fruit_id):
    fruit_id=int(fruit_id)
    ima = Image.open(p_path).convert("RGBA")
    size = ima.size
    print("当前图片大小为:"+str(size[0])+","+str(size[1]))
    yuan_img = Image.open(tmd+"/yuan/"+data[fruit_id]['path']+"/"+data[fruit_id]['file']).convert("RGBA")
    yuan_size = yuan_img.size
    w=yuan_size[0]
    h=yuan_size[1]
    ima = ima.resize((w, h), Image.ANTIALIAS)
    size = ima.size
 
    # 因为是要圆形，所以需要正方形的图片
    r2 = min(size[0], size[1])
    if size[0] != size[1]:
        ima = ima.resize((r2, r2), Image.ANTIALIAS)
 
    # 最后生成圆的半径
    r3 = int(w/2)
    imb = Image.new('RGBA', (r3*2, r3*2),(255,255,255,0))
    pima = ima.load()  # 像素的访问对象
    pimb = imb.load()
    r = float(r2/2) #圆心横坐标
 
    for i in range(r2):
        for j in range(r2):
            lx = abs(i-r) #到圆心距离的横坐标
            ly = abs(j-r)#到圆心距离的纵坐标
            l  = (pow(lx,2) + pow(ly,2))** 0.5  # 三角函数 半径
 
            if l < r3:
                pimb[i-(r-r3),j-(r-r3)] = pima[i,j]
    path=out_path+data[fruit_id]['path']
    os.chdir(cwd)
    if not os.path.isdir(path):
        os.makedirs(path)
    imb.save(path+"/"+data[fruit_id]['file'])
    print("生成成功")